package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RadioButton;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.group.RadioGroup;

public class MedicineDayDialog extends BaseDialog implements TextWatcher {

    private String mText = getContext().getResources().getString(R.string.dialog_medicine_day_long);
    /****************************************************/

    private OnDayChangeListener listener;

    public MedicineDayDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_medicine_day;
    }

    @Override
    protected void initData() {

        findViewById(R.id.layout_dialog_medicine_day_ok).setOnClickListener(v -> cancel());
        findViewById(R.id.layout_dialog_medicine_day_back).setOnClickListener(v -> dismiss());

        final EditText input = findViewById(R.id.dialog_medicine_day_input);
        final RadioGroup group = findViewById(R.id.dialog_medicine_day_group);
        group.setOnCheckedChangeListener((group1, checkedId) -> {

            if (checkedId == R.id.dialog_medicine_day_box9) {
                input.requestFocus();
                InputUtil.openKeybord(input);
            } else {
                input.clearFocus();
                InputUtil.closeKeybord(input);
                final RadioButton button = findViewById(checkedId);
                mText = button.getText().toString().trim();
            }
        });
    }

    @Override
    public void show() {
        super.show();

        final EditText input = findViewById(R.id.dialog_medicine_day_input);
        input.addTextChangedListener(this);
        input.setOnFocusChangeListener((v, hasFocus) -> {
            if (!hasFocus) return;
            final RadioGroup group = findViewById(R.id.dialog_medicine_day_group);
            group.check(R.id.dialog_medicine_day_box9);
        });
    }

    @Override
    public void cancel() {

        final EditText input = findViewById(R.id.dialog_medicine_day_input);
        input.removeTextChangedListener(this);

        if (null != listener) {
            listener.onDayChange(mText);
        }
        super.cancel();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        mText = s.toString().trim() + "天";
        final RadioGroup group = findViewById(R.id.dialog_medicine_day_group);
        group.check(R.id.dialog_medicine_day_box9);
    }

    public void setOnDayChangeListener(OnDayChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDayChangeListener {
        void onDayChange(String day);
    }
}
